** This file produces Figures 3.1, 3.4, 3.5, 3.6, 3.7, 3.9

* Open dataset on sources of contributions to candidates from NIMSP
use newnimsp.dta, clear

* Recode the year variable so that state's holding elections in odd years are 
* placed on next even year to maintain comparability with most states 
gen odd = mod(year,2)
replace year=year+1 if odd==1

* Merge in dataset on campaign finance laws
sort year statabbr
merge year statabbr using masterdataset
drop if _m<3
drop _m

* Rename variable indicating whether state limited contributions from parties
* to candidates
ren l_party_cand l_party

* Drop Nebraska since it holds nonpartisan elections
drop if statabbr =="NE"

* Generate an indicator for states with robust public funding systems
gen pubfundstate=1 if statabbr=="AZ" & year>1998
replace pubfundstate=1 if statabbr=="CT" & year>2006
replace pubfundstate=1 if statabbr=="ME" & year>1998
replace pubfundstate=1 if statabbr=="MN"
recode pubfundstate .=0

* Save this dataset so that it can be re-opened to create multiple graphs
save temp, replace


************** Figure 3.1 *******************
* Make graph for all candidates

use temp, clear

* Collapse data so that it is aggregated by year
collapse (sum) total business issuegroups parties unions law_lob individuals, by(statabbr year)

* Only keep 2006 election cycle -- most complete recent election in terms of data
keep if year==2006

* Generate variables indicating the proportion of funds coming from various sources
gen prop_party=parties/total
gen prop_business=business/total
gen prop_unions=unions/total
gen prop_ind=(law_lob+individuals)/total
gen prop_oth=1-((parties+business+unions+law_lob+individuals)/total)

* Sort states according to how much candidates receive from parties
sort prop_party

* Generate a variable so that states in graph will be ordered by the amount they
* receive from parties
gen xaxis=_n
labmask xaxis, values(statabbr)

* Create Figure 3.1
graph bar prop_party prop_business prop_unions prop_ind prop_oth if xaxis<25, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend(col(2) size(medsmall) symx(12.5) colg(10) region(lc(white)) order(1 "Party" 2 "Business")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5) saving(group1.gph, replace)
graph bar prop_party prop_business prop_unions prop_ind prop_oth if xaxis>24, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend( col(2) size(medsmall) symx(9.5) colg(10) region(lc(white)) order( 3 "Unions" 4 "Individuals")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5)  saving(group2.gph, replace)
graph combine group1.gph group2.gph
graph export fig_3_1.eps, replace


******************* Figure 3.4 *******************************
* Make graph for total amount going to each type of candidate

* Generate separate variables for total amount going to each candidate type
gen incumbent_total=total if incumbency_statusid=="I"
gen challenger_total=total if incumbency_statusid=="C"
gen open_total=total if incumbency_statusid=="O"

* Collapse to create totals by state and year
collapse (sum) incumbent_total open_total challenger_total total, by(statabbr year)

* Keep only the 2006 election cycle
keep if year==2006

* Generate proportion of total going to each type of candidate
gen prop_inc=incumbent_total/total
gen prop_open=open_total/total
gen prop_chall=challenger_total/total

* Sort by proportion of total going to incumbents
sort prop_inc

* Generate a variable to put states in order by the total going to incumbents
gen xaxis=_n
labmask xaxis, values(statabbr)

* Create Figure 3.4
graph bar prop_inc prop_open prop_chall if xaxis<25, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend(col(2) size(medsmall) symx(9.5) colg(4) region(lc(white)) order(1 "Incumbents" 3 "Challengers")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(50)) bar(3, col(gs0) fi(100)) aspect(1.5) saving(group1.gph, replace)
graph bar prop_inc prop_open prop_chall if xaxis>24, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend( col(2) size(medsmall) symx(9.5) colg(14) region(lc(white)) order( 2 "Open-Seat Candidates")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(50)) bar(3, col(gs0) fi(100)) aspect(1.5)  saving(group2.gph, replace)
graph combine group1.gph group2.gph, imargin(tiny)
graph export fig_3_4.eps, replace



************** Figure 3.5 *******************
* Make graph for incumbents

* Keep only candidates who are incumbents
keep if incumbency_statusid =="I"

* Repeat commands from previous section to create Figure 3.5
collapse (sum) total business issuegroups parties unions law_lob individuals, by(statabbr year)
 keep if year==2006
gen prop_party=parties/total
gen prop_business=business/total
gen prop_unions=unions/total
gen prop_ind=(law_lob+individuals)/total
gen prop_oth=1-((parties+business+unions+law_lob+individuals)/total)
sort prop_party
gen xaxis=_n
labmask xaxis, values(statabbr)
graph bar prop_party prop_business prop_unions prop_ind prop_oth if xaxis<25, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend(col(2) size(medsmall) symx(12.5) colg(10) region(lc(white)) order(1 "Party" 2 "Business")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5) saving(group1.gph, replace)
graph bar prop_party prop_business prop_unions prop_ind prop_oth if xaxis>24, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend( col(2) size(medsmall) symx(9.5) colg(10) region(lc(white)) order( 3 "Unions" 4 "Individuals")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5)  saving(group2.gph, replace)
graph combine group1.gph group2.gph, imargin(tiny)
graph export fig_3_5.eps, replace


************** Figure 3.6 *******************
* Make graph for challengers 

* Keep only candidates who are incumbents
keep if incumbency_statusid =="C"

* Repeat commands from previous section to create Figure 3.6
collapse (sum) total business issuegroups parties unions law_lob individuals, by(statabbr year)
 keep if year==2006
gen prop_party=parties/total
gen prop_business=business/total
gen prop_unions=unions/total
gen prop_ind=(law_lob+individuals)/total
gen prop_oth=1-((parties+business+unions+law_lob+individuals)/total)
sort prop_party
gen xaxis=_n
labmask xaxis, values(statabbr)
graph bar prop_party prop_business prop_unions prop_ind prop_oth if xaxis<25, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend(col(2) size(medsmall) symx(12.5) colg(10) region(lc(white)) order(1 "Party" 2 "Business")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5) saving(group1.gph, replace)
graph bar prop_party prop_business prop_unions prop_ind prop_oth if xaxis>24, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend( col(2) size(medsmall) symx(9.5) colg(10) region(lc(white)) order( 3 "Unions" 4 "Individuals")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5)  saving(group2.gph, replace)
graph combine group1.gph group2.gph, imargin(tiny)
graph export fig_3_6.eps, replace



************** Figure 3.7 *******************
* Make graph for open seat candidates  

* Keep only candidates running for open seats
keep if incumbency_statusid =="O"

* Repeat commands from previous section to create Figure 3.7
collapse (sum) total business issuegroups parties unions law_lob individuals, by(statabbr year)
 keep if year==2006
gen prop_party=parties/total
gen prop_business=business/total
gen prop_unions=unions/total
gen prop_ind=(law_lob+individuals)/total
gen prop_oth=1-((parties+business+unions+law_lob+individuals)/total)
sort prop_party
gen xaxis=_n
labmask xaxis, values(statabbr)
graph bar prop_party prop_business prop_unions prop_ind if xaxis<25, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend(col(2) size(medsmall) symx(12.5) colg(10) region(lc(white)) order(1 "Party" 2 "Business")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5) saving(group1.gph, replace)
graph bar prop_party prop_business prop_unions prop_ind if xaxis>24, plotr(lc(white)) over(xaxis, lab(labs(small))) stack scheme(s1mono) graphr(c(white)) legend( col(2) size(medsmall) symx(9.5) colg(10) region(lc(white)) order( 3 "Unions" 4 "Individuals")) ylabel(, nogrid labs(small)) hor bar(1, col(gs0) fi(0)) bar(2, col(gs0) fi(33)) bar(3, col(gs0) fi(67)) bar(4, col(gs0) fi(100)) bar(5, col(gs16)) aspect(1.5)  saving(group2.gph, replace)
graph combine group1.gph group2.gph
graph export fig_3_7.eps, replace



************** Figure 3.9 *******************

* Generate a numerical variable capturing the type of candidate (incumbent, challenger, open seat)
encode incumbency_statusid, gen(candtype)

* Estimate mean amount from each source depending on type of candidate and whether
* parties are limited in giving. Then save each set of mean calculations in its 
* own dataset for graphing. (Need to install parmest package)
mean prop_from_party if  pubfundstate~=1, over( candtype l_party)
parmest, saving(limits_graph2, replace)
mean prop_from_ind if  pubfundstate~=1, over( candtype l_party)
parmest, saving(limits_graph3, replace)
mean prop_from_business if  pubfundstate~=1, over( candtype l_party)
parmest, saving(limits_graph4, replace)
mean prop_from_unions if  pubfundstate~=1, over( candtype l_party)
parmest, saving(limits_graph5, replace)
mean prop_from_issue if  pubfundstate~=1, over( candtype l_party)
parmest, saving(limits_graph7, replace)

* Open the dataset of means for funds raised from parties and create bar graph
use limits_graph2, clear
gen xaxis=_n
recode xaxis 2=1 4=1 6=1 1=0 3=0 5=0, gen(limits)
recode xaxis 1=.9 2=1.1 3=1.9 4=2.1 5=2.9 6=3.1
twoway bar estimate xaxis if limits==0, barw(.2) || bar estimate xaxis if limits==1, barw(.2) ylabel(0 .05 .1 .15 .2) scheme(s1mono) xlabel(1 "Challengers" 2 "Incumbents" 3 `" "Open-Seat" "Candidates" "', labs(small)) xtitle(" ") legend(lab(1 "No Party limits") lab(2 "Party Limits") size(medsmall) col(1)) ytitle("Proportion") title("From Party") saving(propparty.gph, replace) aspect(.8)

* Open the dataset of means for funds raised from individuals and create bar graph
use limits_graph3, clear
gen xaxis=_n
recode xaxis 2=1 4=1 6=1 1=0 3=0 5=0, gen(limits)
recode xaxis 1=.9 2=1.1 3=1.9 4=2.1 5=2.9 6=3.1
twoway bar estimate xaxis if limits==0, barw(.2) || bar estimate xaxis if limits==1, barw(.2) ylabel(0  .1 .2 .3 .4 .5) scheme(s1mono) xlabel(1 "Challengers" 2 "Incumbents" 3 `" "Open-Seat" "Candidates" "', labs(small)) xtitle(" ") legend(lab(1 "No Party limits") lab(2 "Party Limits") size(medsmall) col(1)) ytitle("Proportion") title("From Individuals")  saving(propind.gph, replace) aspect(.8)

* Open the dataset of means for funds raised from businesses and create bar graph
use limits_graph4, clear
gen xaxis=_n
recode xaxis 2=1 4=1 6=1 1=0 3=0 5=0, gen(limits)
recode xaxis 1=.9 2=1.1 3=1.9 4=2.1 5=2.9 6=3.1
twoway bar estimate xaxis if limits==0, barw(.2) || bar estimate xaxis if limits==1, barw(.2) ylabel(0 .1 .2 .3 .4 .5 .6) scheme(s1mono) xlabel(1 "Challengers" 2 "Incumbents" 3 `" "Open-Seat" "Candidates" "', labs(small)) xtitle(" ") legend(lab(1 "No Party limits") lab(2 "Party Limits") size(medsmall) col(1)) ytitle("Proportion") title("From Business")  saving(propbus.gph, replace) aspect(.8)

* Open the dataset of means for funds raised from unions and create bar graph
use limits_graph5, clear
gen xaxis=_n
recode xaxis 2=1 4=1 6=1 1=0 3=0 5=0, gen(limits)
recode xaxis 1=.9 2=1.1 3=1.9 4=2.1 5=2.9 6=3.1
twoway bar estimate xaxis if limits==0, barw(.2) || bar estimate xaxis if limits==1, barw(.2) ylabel(0 .05 .1 .15 .2) scheme(s1mono) xlabel(1 "Challengers" 2 "Incumbents" 3 `" "Open-Seat" "Candidates" "', labs(small)) xtitle(" ") legend(lab(1 "No Party limits") lab(2 "Party Limits") size(medsmall) col(1)) ytitle("Proportion") title("From Labor")  saving(proplabor.gph, replace) aspect(.8)

* Open the dataset of means for funds raised from unions and create bar graph
use limits_graph6, clear
gen xaxis=_n
recode xaxis 2=1 4=1 6=1 1=0 3=0 5=0, gen(limits)
recode xaxis 1=.9 2=1.1 3=1.9 4=2.1 5=2.9 6=3.1
twoway bar estimate xaxis if limits==0, barw(.2) || bar estimate xaxis if limits==1, barw(.2) ylabel(0 .05 .1 .15 .2) scheme(s1mono) xlabel(1 "Challengers" 2 "Incumbents" 3 `" "Open-Seat" "Candidates" "', labs(small)) xtitle(" ") legend(lab(1 "No Party limits") lab(2 "Party Limits") size(medsmall) col(1)) ytitle("Proportion") title("From Laweyers")  saving(proplaw.gph, replace) aspect(.8)

* Open the dataset of means for funds raised from issue groups and create bar graph
use limits_graph7, clear
gen xaxis=_n
recode xaxis 2=1 4=1 6=1 1=0 3=0 5=0, gen(limits)
recode xaxis 1=.9 2=1.1 3=1.9 4=2.1 5=2.9 6=3.1
twoway bar estimate xaxis if limits==0, barw(.2) || bar estimate xaxis if limits==1, barw(.2) ylabel(0 .05 .1 .15 .2) scheme(s1mono) xlabel(1 "Challengers" 2 "Incumbents" 3 `" "Open-Seat" "Candidates" "', labs(small)) xtitle(" ") legend(lab(1 "No Party limits") lab(2 "Party Limits") size(medsmall) col(1)) ytitle("Proportion") title("From Issue Groups")  saving(propissue.gph, replace) aspect(.8)

* Combine graphs created above into a single graph and create Figure 3.9
graph combine propparty.gph propind.gph  propbus.gph proplabor.gph propissue.gph, graphr(c(white)) imargin(tiny)
graph export fig_3_9.eps, replace






